package com.example.demo.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.common.Func;
import com.example.demo.common.Http;
import com.example.demo.common.constant.HttpFlag;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.junit.Test;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLEncoder;
import java.util.Base64;

@RestController
@RequestMapping("/interface")
@Api(tags = "3、爬虫管理")
@ApiSupport(author = "罗海林", order = 3)
@Validated
@Slf4j
public class InterfaceController {

    //@PostMapping("/test1")
    @Test
    public void test1() {
        String url = "http://ggzy.jgswj.gxzf.gov.cn/inteligentsearchgxes/rest/esinteligentsearch/getFullTextDataNew";

        JSONObject condition = new JSONObject();
        condition.put("fieldName", "categorynum");
        condition.put("equal", "001001");
        condition.put("notEqual", null);
        condition.put("equalList", null);
        condition.put("notEqualList", null);
        condition.put("isLike", true);
        condition.put("likeType", "2");

        JSONArray list = new JSONArray();
        list.add(condition);

        JSONObject param = new JSONObject();
        param.put("token", "");
        param.put("pn", "0");
        param.put("rn", "15");
        param.put("sdt", "");
        param.put("edt", "");
        param.put("wd", "");
        param.put("inc_wd", "");
        param.put("exc_wd", "");
        param.put("fields", "title");
        param.put("cnum", "006");
        param.put("sort", "{\"infodatepx\":\"0\"}");
        param.put("ssort", "title");
        param.put("cl", "200");
        param.put("terminal", "");
        param.put("time", null);
        param.put("highlights", "");
        param.put("statistics", null);
        param.put("unionCondition", null);
        param.put("accuracy", "");
        param.put("noParticiple", "0");
        param.put("searchRange", null);
        param.put("isBusiness", "1");
        param.put("condition", list);


        JSONObject header = new JSONObject();
        header.put("Referer", "http://ggzy.jgswj.gxzf.gov.cn/bhggzy/jyxx/001001/001001002/001001002001/tradeInfo.html");
        header.put("Origin", "http://ggzy.jgswj.gxzf.gov.cn");
        header.put("Host", "ggzy.jgswj.gxzf.gov.cn");
        header.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.31");
        header.put("X-Requested-With", "XMLHttpRequest");
        header.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        header.put("Connection", "keep-alive");
        header.put("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6");
        header.put("Accept-Encoding", "gzip, deflate");
        header.put("Accept", "application/json, text/javascript, */*; q=0.01");
        header.put("Cookie", "_trs_uv=lht8wchx_3928_e480; _zcy_log_client_uuid=ce8c1380-0abc-11ee-a51f-27392c287cb4; oauthClientId=demoClient; oauthPath=http://10.5.25.30:8080/GgzyjyWebBuilder; oauthLoginUrl=http://10.5.25.30:8080/GgzyjyWebBuilder/rest/oauth2/authorize?client_id=demoClient&state=a&response_type=code&scope=user&redirect_uri=; oauthLogoutUrl=http://10.5.25.30:8080/GgzyjyWebBuilder/rest/oauth2/logout?redirect_uri=; noOauthRefreshToken=22f26729fa841c1510efa63c490c5f02; noOauthAccessToken=29a71497a85950e229f36d8696c77cd7");


        String js = Http.post(url, param, header, HttpFlag.RAW_JSON, null);
        Func.print(js);

    }

    @Test
    public void test2() {
        String url = "http://ggzy.jgswj.gxzf.gov.cn/EWB-FRONT/rest/GgSearchAction/getCategoryList";

        JSONObject condition = new JSONObject();
        condition.put("siteGuid", "37b8c7a5-3090-481f-bf12-0c78ef922362");
        condition.put("categoryNum", "001001");

        JSONObject param = new JSONObject();
        param.put("params", condition);

        String js = Http.post(url, param, null, null, null);
        Func.print(js);
    }

    @Test
    public void test3() {
        String url = "http://ggzy.jgswj.gxzf.gov.cn/bhggzy/jyxx/001001/001001002/001001002004/20230920/74c91262-e9da-4fb7-8431-3d07aea8766e.html";

        String resHtml = Http.get(url, null);

        Document doc = Jsoup.parse(resHtml);
        Element div = doc.body().getElementsByClass("ewb-details-info").get(0);
        resHtml = div.html();

        Func.print(resHtml);
    }

    @Test
    public void test4() {
        String url = "http://zfcg.gxzf.gov.cn/portal/category";

        JSONArray array = new JSONArray();
        array.add("450500");

        JSONObject param = new JSONObject();
        param.put("categoryCode", "ZcyAnnouncement1");
        param.put("leaf", "0");
        param.put("pageNo", "1");
        param.put("pageSize", "15");
        param.put("_t", "1695628491000");
        param.put("districtCode", array);

        JSONObject header = new JSONObject();
        header.put("Content-Type", "application/json;charset=utf-8");
        header.put("Accept", "application/json, text/javascript, */*; q=0.01");


        String result = Http.post(url, param, header, HttpFlag.RAW_JSON, null);
        Func.print(result);
    }

    @Test
    public void test5() {
        String url = "http://zfcg.gxzf.gov.cn/api/core/remote/announcementDistrict?queryWithCode=true&includeSelf=false&includeAllNode=false&filterNotActive=true&districtCode=450500&timestamp=1695719702";

        String result = Http.get(url, null);
        Func.print(result);
    }

    @Test
    public void test6() {
        String res = encrypt("luohsilinluohsilinluohsilinluohsilinluohsilinluohsilinluohsilin", "");
        Func.print(res);
    }

    private String encrypt(String txt, String key) {
        txt = txt + key;
        String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
        int nh = 18;
        String ch = String.valueOf(chars.charAt(nh));
        String mdKey = DigestUtils.md5Hex(key + ch);
        mdKey = mdKey.substring(nh % 8, nh % 8 + 7);
        mdKey = "449120506";

        byte[] bytes = txt.getBytes();
        txt = Base64.getEncoder().encodeToString(bytes) + "YWJj";

        String tmp = "";
        int i = 0, j = 0, k = 0;
        for (i = 0; i < txt.length(); i++) {
            k = k == mdKey.length() ? 0 : k;
            j = (nh + chars.indexOf(txt.charAt(i))) + Integer.valueOf(mdKey.charAt(k++)) % 64;
            if (j < chars.length()) {
                tmp += String.valueOf(chars.charAt(j));
            }
        }

        tmp = ch + tmp;
        bytes = tmp.getBytes();
        tmp = Base64.getEncoder().encodeToString(bytes);
        return URLEncoder.encode(tmp);
    }

}
